package com.tongtong.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class Json2ExcelUtils {

    /**
     * json 2 excel 多sheet页工具类
     * @param sheets sheet 页名称
     * @param maps  sheet页数据
     * @param type  1 为 xls 2 为 xlsx
     * @return
     * @throws IOException
     */
    public static Boolean createSheetsExcel(List<String> sheets, Map<String,List<Map>> maps, Integer type,String path) {
        Workbook wb = null;
        try{
            if(type == 1){
                wb = new HSSFWorkbook();
            }
            if(type==2){
                wb = new XSSFWorkbook();
            }
            for (int i = 0; i < sheets.size(); i++) {
                String name = sheets.get(i);
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(maps.get(name)));
                jsonToExcel(wb,jsonArray,name);
            }
            FileOutputStream fileOut = new FileOutputStream(path);
            wb.write(fileOut);
        }catch (IOException e){
            e.printStackTrace();
            return false;
        }finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 单个sheet页的excel表格
     * @param sheets 名称
     * @param maps 数据
     * @param type 类型
     * @param path 路径
     * @return
     */
    public static Boolean createSheetExcel(String sheets,List<Map> maps,Integer type,String path){
        Map<String,List<Map>> list = new HashMap<>();
        list.put(sheets,maps);
        return createSheetsExcel(Arrays.asList(sheets),list,type,path);
    }


    /**
     * json 转 excel
     * @param jsonArray
     * @return
     * @throws IOException
     */
    private static Workbook jsonToExcel(Workbook wb, JSONArray jsonArray, String sheetName) {
        // 创建Sheet对象
        Sheet sheet = wb.createSheet(sheetName);
        int roleNo = 0;
        int rowNo = 0;
        // 创建Row对象
        Row row = sheet.createRow(roleNo++);
        // 创建表格标题
        Set<String> keys = jsonArray.getJSONObject(0).keySet();
        for (String s : keys) {
            Cell cell = row.createCell(rowNo++);
            cell.setCellValue(s);
        }
        rowNo = 0;
        for (int i = 0; i < jsonArray.size(); i ++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            row = sheet.createRow(roleNo++);
            for (String s : keys) {
                Cell cell = row.createCell(rowNo++);
                cell.setCellValue(jsonObject.getString(s));
            }
            rowNo = 0;
        }
        return wb;
    }
}
